<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>props 初体验</title>
</head>

<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <!-- 1. 引入 prop-type.js -->
    <script src="../js/prop-types.js"></script>

    <script type="text/babel">
        //外部数据
        let obj = {
            name: 90,
            gender: '男'
        };

        let obj2 = {
            name: '隋博', 
            age: 20, 
            gender: '男'
        }

        let obj3 = {
            name: '段佳敏', 
            age: 18, 
            gender: '女'
        }

        //类式组件
        class Person extends React.Component{
            //构造方法  规范写法
            constructor(props){
                super(props);
                //这里访问props属性  如果参数没有设置 props, 这下面无法访问this.props 的值
                console.log(this.props);
            }
            
            //2. 声明静态属性
            static propTypes = {
                //isRequired 用来限制该属性值必须要设置
                age: PropTypes.number.isRequired, //限制 age 的属性值必须是数字  
                gender: PropTypes.string, //限制gender 的属性值必须是字符串
                name: PropTypes.string,
            }

            //2-2 设置属性值的默认值  default 默认  props properties 的缩写
            static defaultProps = {
                age: 18
            }

            render(){
                // console.log(this.props);
                //解构赋值
                let {name, age, gender} = this.props;
                //返回虚拟 DOM 对象
                return <ul>
                            <li>姓名: {name}</li>
                            <li>年龄: {age}</li>
                            <li>性别: {gender}</li>
                        </ul>
            }
        }

        //渲染
        ReactDOM.render(<div>
                <Person name={obj.name} age={obj.age} gender={obj.gender}  />
                <Person {...obj2}  />
                <Person {...obj3}  />
            </div>
            , document.querySelector('#root'));
    </script>
</body>

</html>